********************************************************************************
**  TITLE:      qc2022_datagochi					                          **  
**  AUTHORS:    P. Mongrain, N. Fréchet, B. Thompson Collart, and Y. Dufresne **
**  DATA:       datagochi2022								                  **
**	DATE:		February 2025	 					                          **	
**  VERSION:	Stata 17					                                  **	
********************************************************************************

* Note: The data file needs to be downloaded from its original source (see the "surveys.text" file)

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "qc2022_datagochi", replace

* Open the dataset

use "datagochi2022.dta", clear

* Election

gen election = "qc2022"

* Type of survey

gen type = "datagochi"

* Respondent ID

gen responseid = id

* Survey weight

merge 1:1 election responseid using qc2022_weight
drop _merge


***********************
** DISTRICT OUTCOMES **
***********************

* District codes

gen district_code = .

replace district_code = 648 if riding == "Abitibi Est"
replace district_code = 642 if riding == "Abitibi Ouest"
replace district_code = 338 if riding == "Rouyn Noranda Témiscamingue"
replace district_code = 366 if riding == "Matane Matapédia"
replace district_code = 520 if riding == "Rimouski"
replace district_code = 144 if riding == "Rivière du Loup Témiscouata"
replace district_code = 806 if riding == "Charlesbourg"
replace district_code = 802 if riding == "Charlevoix Côte de Beaupré"
replace district_code = 218 if riding == "Chauveau"
replace district_code = 822 if riding == "Jean Lesage"
replace district_code = 566 if riding == "Jean Talon"
replace district_code = 588 if riding == "La Peltrie"
replace district_code = 536 if riding == "Louis Hébert"
replace district_code = 850 if riding == "Montmorency"
replace district_code = 258 if riding == "Portneuf"
replace district_code = 360 if riding == "Taschereau"
replace district_code = 370 if riding == "Vanier Les Rivières"
replace district_code = 204 if riding == "Arthabaska"
replace district_code = 238 if riding == "Drummond Bois Francs"
replace district_code = 676 if riding == "Nicolet Bécancour"
replace district_code = 620 if riding == "Beauce Nord"
replace district_code = 748 if riding == "Beauce Sud"
replace district_code = 760 if riding == "Bellechasse"
replace district_code = 226 if riding == "Chutes de la Chaudière"
replace district_code = 754 if riding == "Côte du Sud"
replace district_code = 918 if riding == "Lévis"
replace district_code = 460 if riding == "Lotbinière Frontenac"
replace district_code = 814 if riding == "Duplessis"
replace district_code = 826 if riding == "René Lévesque"
replace district_code = 320 if riding == "Mégantic"
replace district_code = 508 if riding == "Orford"
replace district_code = 138 if riding == "Richmond"
replace district_code = 914 if riding == "Saint François"
replace district_code = 902 if riding == "Sherbrooke"
replace district_code = 466 if riding == "Bonaventure"
replace district_code = 854 if riding == "Gaspé"
replace district_code = 614 if riding == "Îles de la Madeleine"
replace district_code = 346 if riding == "Berthier"
replace district_code = 206 if riding == "Joliette"
replace district_code = 502 if riding == "L'Assomption"
replace district_code = 352 if riding == "Masson"
replace district_code = 602 if riding == "Repentigny"
replace district_code = 216 if riding == "Rousseau"
replace district_code = 210 if riding == "Terrebonne"
replace district_code = 858 if riding == "Argenteuil"
replace district_code = 310 if riding == "Bertrand"
replace district_code = 736 if riding == "Blainville"
replace district_code = 364 if riding == "Deux Montagnes"
replace district_code = 702 if riding == "Groulx"
replace district_code = 126 if riding == "Labelle"
replace district_code = 570 if riding == "Les Plaines"
replace district_code = 922 if riding == "Mirabel"
replace district_code = 594 if riding == "Prévost"
replace district_code = 926 if riding == "Saint Jérôme"
replace district_code = 390 if riding == "Chomedey"
replace district_code = 720 if riding == "Fabre"
replace district_code = 236 if riding == "Laval des Rapides"
replace district_code = 244 if riding == "Mille Îles"
replace district_code = 232 if riding == "Sainte Rose"
replace district_code = 544 if riding == "Vimont"
replace district_code = 344 if riding == "Champlain"
replace district_code = 454 if riding == "Laviolette Saint Maurice"
replace district_code = 670 if riding == "Maskinongé"
replace district_code = 530 if riding == "Trois Rivières"
replace district_code = 818 if riding == "Beauharnois"
replace district_code = 810 if riding == "Borduas"
replace district_code = 708 if riding == "Brome Missisquoi"
replace district_code = 304 if riding == "Chambly"
replace district_code = 246 if riding == "Châteauguay"
replace district_code = 306 if riding == "Granby"
replace district_code = 666 if riding == "Huntingdon"
replace district_code = 548 if riding == "Iberville"
replace district_code = 842 if riding == "Johnson"
replace district_code = 340 if riding == "La Pinière"
replace district_code = 104 if riding == "La Prairie"
replace district_code = 350 if riding == "Laporte"
replace district_code = 482 if riding == "Marie Victorin"
replace district_code = 514 if riding == "Montarville"
replace district_code = 252 if riding == "Richelieu"
replace district_code = 742 if riding == "Saint Hyacinthe"
replace district_code = 336 if riding == "Saint Jean"
replace district_code = 312 if riding == "Sanguinet"
replace district_code = 150 if riding == "Soulanges"
replace district_code = 324 if riding == "Taillon"
replace district_code = 120 if riding == "Vachon"
replace district_code = 626 if riding == "Vaudreuil"
replace district_code = 380 if riding == "Verchères"
replace district_code = 608 if riding == "Anjou Louis Riel"
replace district_code = 714 if riding == "Bourassa Sauvé"
replace district_code = 582 if riding == "Camille Laurin"
replace district_code = 906 if riding == "Gouin"
replace district_code = 560 if riding == "Hochelaga Maisonneuve"
replace district_code = 264 if riding == "Jeanne Mance Viger"
replace district_code = 132 if riding == "LaFontaine"
replace district_code = 838 if riding == "Laurier Dorion"
replace district_code = 834 if riding == "Maurice Richard"
replace district_code = 316 if riding == "Mercier"
replace district_code = 930 if riding == "Pointe aux Trembles"
replace district_code = 356 if riding == "Rosemont"
replace district_code = 576 if riding == "Sainte Marie Saint Jacques"
replace district_code = 636 if riding == "Viau"
replace district_code = 110 if riding == "Acadie"
replace district_code = 326 if riding == "D'Arcy McGee"
replace district_code = 260 if riding == "Jacques Cartier"
replace district_code = 212 if riding == "Marguerite Bourgeoys"
replace district_code = 526 if riding == "Marquette"
replace district_code = 318 if riding == "Mont Royal Outremont"
replace district_code = 330 if riding == "Nelligan"
replace district_code = 470 if riding == "Notre Dame de Grâce"
replace district_code = 230 if riding == "Robert Baldwin"
replace district_code = 116 if riding == "Saint Henri Sainte Anne"
replace district_code = 220 if riding == "Saint Laurent"
replace district_code = 250 if riding == "Verdun"
replace district_code = 730 if riding == "Westmount Saint Louis"
replace district_code = 542 if riding == "Ungava"
replace district_code = 660 if riding == "Chapleau"
replace district_code = 938 if riding == "Gatineau"
replace district_code = 240 if riding == "Hull"
replace district_code = 726 if riding == "Papineau"
replace district_code = 224 if riding == "Pontiac"
replace district_code = 256 if riding == "Chicoutimi"
replace district_code = 300 if riding == "Dubuc"
replace district_code = 358 if riding == "Jonquière"
replace district_code = 476 if riding == "Lac Saint Jean"
replace district_code = 332 if riding == "Roberval"

* Election outcome in each district

gen district_outcome = .

replace district_outcome = 241 if riding == "Abitibi Est"
replace district_outcome = 241 if riding == "Abitibi Ouest"
replace district_outcome = 243 if riding == "Acadie"
replace district_outcome = 241 if riding == "Anjou Louis Riel"
replace district_outcome = 241 if riding == "Argenteuil"
replace district_outcome = 241 if riding == "Arthabaska"
replace district_outcome = 241 if riding == "Beauce Nord"
replace district_outcome = 241 if riding == "Beauce Sud"
replace district_outcome = 241 if riding == "Beauharnois"
replace district_outcome = 241 if riding == "Bellechasse"
replace district_outcome = 241 if riding == "Berthier"
replace district_outcome = 241 if riding == "Bertrand"
replace district_outcome = 241 if riding == "Blainville"
replace district_outcome = 241 if riding == "Bonaventure"
replace district_outcome = 241 if riding == "Borduas"
replace district_outcome = 243 if riding == "Bourassa Sauvé"
replace district_outcome = 241 if riding == "Brome Missisquoi"
replace district_outcome = 242 if riding == "Camille Laurin"
replace district_outcome = 241 if riding == "Chambly"
replace district_outcome = 241 if riding == "Champlain"
replace district_outcome = 241 if riding == "Chapleau"
replace district_outcome = 241 if riding == "Charlesbourg"
replace district_outcome = 241 if riding == "Charlevoix Côte de Beaupré"
replace district_outcome = 241 if riding == "Châteauguay"
replace district_outcome = 241 if riding == "Chauveau"
replace district_outcome = 241 if riding == "Chicoutimi"
replace district_outcome = 243 if riding == "Chomedey"
replace district_outcome = 241 if riding == "Chutes de la Chaudière"
replace district_outcome = 241 if riding == "Côte du Sud"
replace district_outcome = 243 if riding == "D'Arcy McGee"
replace district_outcome = 241 if riding == "Deux Montagnes"
replace district_outcome = 241 if riding == "Drummond Bois Francs"
replace district_outcome = 241 if riding == "Dubuc"
replace district_outcome = 241 if riding == "Duplessis"
replace district_outcome = 241 if riding == "Fabre"
replace district_outcome = 241 if riding == "Gaspé"
replace district_outcome = 241 if riding == "Gatineau"
replace district_outcome = 244 if riding == "Gouin"
replace district_outcome = 241 if riding == "Granby"
replace district_outcome = 241 if riding == "Groulx"
replace district_outcome = 244 if riding == "Hochelaga Maisonneuve"
replace district_outcome = 241 if riding == "Hull"
replace district_outcome = 241 if riding == "Huntingdon"
replace district_outcome = 241 if riding == "Iberville"
replace district_outcome = 242 if riding == "Îles de la Madeleine"
replace district_outcome = 243 if riding == "Jacques Cartier"
replace district_outcome = 244 if riding == "Jean Lesage"
replace district_outcome = 241 if riding == "Jean Talon"
replace district_outcome = 243 if riding == "Jeanne Mance Viger"
replace district_outcome = 241 if riding == "Johnson"
replace district_outcome = 241 if riding == "Joliette"
replace district_outcome = 241 if riding == "Jonquière"
replace district_outcome = 241 if riding == "La Peltrie"
replace district_outcome = 243 if riding == "La Pinière"
replace district_outcome = 241 if riding == "La Prairie"
replace district_outcome = 241 if riding == "Labelle"
replace district_outcome = 241 if riding == "Lac Saint Jean"
replace district_outcome = 243 if riding == "LaFontaine"
replace district_outcome = 241 if riding == "Laporte"
replace district_outcome = 241 if riding == "L'Assomption"
replace district_outcome = 244 if riding == "Laurier Dorion"
replace district_outcome = 241 if riding == "Laval des Rapides"
replace district_outcome = 241 if riding == "Laviolette Saint Maurice"
replace district_outcome = 241 if riding == "Les Plaines"
replace district_outcome = 241 if riding == "Lévis"
replace district_outcome = 241 if riding == "Lotbinière Frontenac"
replace district_outcome = 241 if riding == "Louis Hébert"
replace district_outcome = 243 if riding == "Marguerite Bourgeoys"
replace district_outcome = 241 if riding == "Marie Victorin"
replace district_outcome = 243 if riding == "Marquette"
replace district_outcome = 241 if riding == "Maskinongé"
replace district_outcome = 241 if riding == "Masson"
replace district_outcome = 242 if riding == "Matane Matapédia"
replace district_outcome = 244 if riding == "Maurice Richard"
replace district_outcome = 241 if riding == "Mégantic"
replace district_outcome = 244 if riding == "Mercier"
replace district_outcome = 243 if riding == "Mille Îles"
replace district_outcome = 241 if riding == "Mirabel"
replace district_outcome = 243 if riding == "Mont Royal Outremont"
replace district_outcome = 241 if riding == "Montarville"
replace district_outcome = 241 if riding == "Montmorency"
replace district_outcome = 243 if riding == "Nelligan"
replace district_outcome = 241 if riding == "Nicolet Bécancour"
replace district_outcome = 243 if riding == "Notre Dame de Grâce"
replace district_outcome = 241 if riding == "Orford"
replace district_outcome = 241 if riding == "Papineau"
replace district_outcome = 241 if riding == "Pointe aux Trembles"
replace district_outcome = 243 if riding == "Pontiac"
replace district_outcome = 241 if riding == "Portneuf"
replace district_outcome = 241 if riding == "Prévost"
replace district_outcome = 241 if riding == "René Lévesque"
replace district_outcome = 241 if riding == "Repentigny"
replace district_outcome = 241 if riding == "Richelieu"
replace district_outcome = 241 if riding == "Richmond"
replace district_outcome = 241 if riding == "Rimouski"
replace district_outcome = 241 if riding == "Rivière du Loup Témiscouata"
replace district_outcome = 243 if riding == "Robert Baldwin"
replace district_outcome = 241 if riding == "Roberval"
replace district_outcome = 244 if riding == "Rosemont"
replace district_outcome = 241 if riding == "Rousseau"
replace district_outcome = 241 if riding == "Rouyn Noranda Témiscamingue"
replace district_outcome = 241 if riding == "Saint François"
replace district_outcome = 243 if riding == "Saint Henri Sainte Anne"
replace district_outcome = 241 if riding == "Saint Hyacinthe"
replace district_outcome = 241 if riding == "Saint Jean"
replace district_outcome = 241 if riding == "Saint Jérôme"
replace district_outcome = 243 if riding == "Saint Laurent"
replace district_outcome = 244 if riding == "Sainte Marie Saint Jacques"
replace district_outcome = 241 if riding == "Sainte Rose"
replace district_outcome = 241 if riding == "Sanguinet"
replace district_outcome = 244 if riding == "Sherbrooke"
replace district_outcome = 241 if riding == "Soulanges"
replace district_outcome = 241 if riding == "Taillon"
replace district_outcome = 244 if riding == "Taschereau"
replace district_outcome = 241 if riding == "Terrebonne"
replace district_outcome = 241 if riding == "Trois Rivières"
replace district_outcome = 241 if riding == "Ungava"
replace district_outcome = 241 if riding == "Vachon"
replace district_outcome = 241 if riding == "Vanier Les Rivières"
replace district_outcome = 243 if riding == "Vaudreuil"
replace district_outcome = 241 if riding == "Verchères"
replace district_outcome = 244 if riding == "Verdun"
replace district_outcome = 243 if riding == "Viau"
replace district_outcome = 241 if riding == "Vimont"
replace district_outcome = 243 if riding == "Westmount Saint Louis"

label define outcome 241 "Coalition Avenir Québec" 242 "Parti Quebecois" 243 "Quebec Liberal Party" 244 "Quebec solidaire" 245 "Parti conservateur du Quebec"
label values district_outcome outcome


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 32.46 if riding == "Abitibi Est"
replace margin = 25.99 if riding == "Abitibi Ouest"
replace margin = 14.22 if riding == "Rouyn Noranda Témiscamingue"
replace margin = 50.07 if riding == "Matane Matapédia"
replace margin = 13.11 if riding == "Rimouski"
replace margin = 34.48 if riding == "Rivière du Loup Témiscouata"
replace margin = 24.63 if riding == "Charlesbourg"
replace margin = 30.05 if riding == "Charlevoix Côte de Beaupré"
replace margin = 15.00 if riding == "Chauveau"
replace margin = 6.51 if riding == "Jean Lesage"
replace margin = 8.74 if riding == "Jean Talon"
replace margin = 14.45 if riding == "La Peltrie"
replace margin = 30.70 if riding == "Louis Hébert"
replace margin = 19.12 if riding == "Montmorency"
replace margin = 17.64 if riding == "Portneuf"
replace margin = 16.96 if riding == "Taschereau"
replace margin = 27.87 if riding == "Vanier Les Rivières"
replace margin = 27.06 if riding == "Arthabaska"
replace margin = 34.25 if riding == "Drummond Bois Francs"
replace margin = 24.82 if riding == "Nicolet Bécancour"
replace margin = 0.60 if riding == "Beauce Nord"
replace margin = 1.15 if riding == "Beauce Sud"
replace margin = 10.48 if riding == "Bellechasse"
replace margin = 20.26 if riding == "Chutes de la Chaudière"
replace margin = 24.28 if riding == "Côte du Sud"
replace margin = 28.04 if riding == "Lévis"
replace margin = 11.52 if riding == "Lotbinière Frontenac"
replace margin = 20.35 if riding == "Duplessis"
replace margin = 37.75 if riding == "René Lévesque"
replace margin = 23.92 if riding == "Mégantic"
replace margin = 27.96 if riding == "Orford"
replace margin = 26.89 if riding == "Richmond"
replace margin = 14.22 if riding == "Saint François"
replace margin = 6.66 if riding == "Sherbrooke"
replace margin = 14.39 if riding == "Bonaventure"
replace margin = 3.9 if riding == "Gaspé"
replace margin = 6.44 if riding == "Îles de la Madeleine"
replace margin = 30.15 if riding == "Berthier"
replace margin = 14.35 if riding == "Joliette"
replace margin = 44.31 if riding == "L'Assomption"
replace margin = 33.36 if riding == "Masson"
replace margin = 34.63 if riding == "Repentigny"
replace margin = 32.72 if riding == "Rousseau"
replace margin = 30.56 if riding == "Terrebonne"
replace margin = 28.89 if riding == "Argenteuil"
replace margin = 24.63 if riding == "Bertrand"
replace margin = 34.02 if riding == "Blainville"
replace margin = 29.92 if riding == "Deux Montagnes"
replace margin = 31.54 if riding == "Groulx"
replace margin = 33.95 if riding == "Labelle"
replace margin = 34.41 if riding == "Les Plaines"
replace margin = 33.33 if riding == "Mirabel"
replace margin = 26.65 if riding == "Prévost"
replace margin = 31.19 if riding == "Saint Jérôme"
replace margin = 9.82 if riding == "Chomedey"
replace margin = 0.90 if riding == "Fabre"
replace margin = 3.17 if riding == "Laval des Rapides"
replace margin = 1.45 if riding == "Mille Îles"
replace margin = 14.52 if riding == "Sainte Rose"
replace margin = 4.43 if riding == "Vimont"
replace margin = 38.34 if riding == "Champlain"
replace margin = 34.97 if riding == "Laviolette Saint Maurice"
replace margin = 37.44 if riding == "Maskinongé"
replace margin = 43.43 if riding == "Trois Rivières"
replace margin = 36.82 if riding == "Beauharnois"
replace margin = 31.77 if riding == "Borduas"
replace margin = 29.96 if riding == "Brome Missisquoi"
replace margin = 30.57 if riding == "Chambly"
replace margin = 14.34 if riding == "Châteauguay"
replace margin = 43.90 if riding == "Granby"
replace margin = 32.25 if riding == "Huntingdon"
replace margin = 37.91 if riding == "Iberville"
replace margin = 37.37 if riding == "Johnson"
replace margin = 7.34 if riding == "La Pinière"
replace margin = 38.86 if riding == "La Prairie"
replace margin = 1.94 if riding == "Laporte"
replace margin = 8.26 if riding == "Marie Victorin"
replace margin = 27.21 if riding == "Montarville"
replace margin = 35.61 if riding == "Richelieu"
replace margin = 37.72 if riding == "Saint Hyacinthe"
replace margin = 31.43 if riding == "Saint Jean"
replace margin = 32.48 if riding == "Sanguinet"
replace margin = 20.8 if riding == "Soulanges"
replace margin = 21.20 if riding == "Taillon"
replace margin = 28.84 if riding == "Vachon"
replace margin = 1.45 if riding == "Vaudreuil"
replace margin = 30.57 if riding == "Verchères"
replace margin = 5.04 if riding == "Anjou Louis Riel"
replace margin = 15.12 if riding == "Bourassa Sauvé"
replace margin = 9.74 if riding == "Camille Laurin"
replace margin = 45.81 if riding == "Gouin"
replace margin = 32.04 if riding == "Hochelaga Maisonneuve"
replace margin = 37.33 if riding == "Jeanne Mance Viger"
replace margin = 31.66 if riding == "LaFontaine"
replace margin = 29.23 if riding == "Laurier Dorion"
replace margin = 7.51 if riding == "Maurice Richard"
replace margin = 39.35 if riding == "Mercier"
replace margin = 26.01 if riding == "Pointe aux Trembles"
replace margin = 14.56 if riding == "Rosemont"
replace margin = 31.84 if riding == "Sainte Marie Saint Jacques"
replace margin = 7.74 if riding == "Viau"
replace margin = 25.06 if riding == "Acadie"
replace margin = 29.46 if riding == "D'Arcy McGee"
replace margin = 51.34 if riding == "Jacques Cartier"
replace margin = 21.63 if riding == "Marguerite Bourgeoys"
replace margin = 24.91 if riding == "Marquette"
replace margin = 19.07 if riding == "Mont Royal Outremont"
replace margin = 35.38 if riding == "Nelligan"
replace margin = 34.97 if riding == "Notre Dame de Grâce"
replace margin = 41.74 if riding == "Robert Baldwin"
replace margin = 8.43 if riding == "Saint Henri Sainte Anne"
replace margin = 35.68 if riding == "Saint Laurent"
replace margin = 1.48 if riding == "Verdun"
replace margin = 37.66 if riding == "Westmount Saint Louis"
replace margin = 18.08 if riding == "Ungava"
replace margin = 38.69 if riding == "Chapleau"
replace margin = 27.18 if riding == "Gatineau"
replace margin = 8.72 if riding == "Hull"
replace margin = 39.05 if riding == "Papineau"
replace margin = 18.98 if riding == "Pontiac"
replace margin = 48.07 if riding == "Chicoutimi"
replace margin = 40.05 if riding == "Dubuc"
replace margin = 40.09 if riding == "Jonquière"
replace margin = 25.85 if riding == "Lac Saint Jean"
replace margin = 35.66 if riding == "Roberval"

* Competition measure

gen competition = .

replace competition = .4584576 if district_code == 104
replace competition = .4239953 if district_code == 110
replace competition = .3309484 if district_code == 116
replace competition = .5104193 if district_code == 120
replace competition = .4640746 if district_code == 126
replace competition = .4701674 if district_code == 132
replace competition = .4642969 if district_code == 138
replace competition = .4193943 if district_code == 144
replace competition = .4449518 if district_code == 150
replace competition = .5143169 if district_code == 204
replace competition = .4472848 if district_code == 206
replace competition = .4703039 if district_code == 210
replace competition = .4624288 if district_code == 212
replace competition = .5112318 if district_code == 216
replace competition = .4630892 if district_code == 218
replace competition = .4844656 if district_code == 220
replace competition = .3930913 if district_code == 224
replace competition = .4659577 if district_code == 226
replace competition = .4923043 if district_code == 230
replace competition = .4799496 if district_code == 232
replace competition = .401134 if district_code == 236
replace competition = .4772792 if district_code == 238
replace competition = .504976 if district_code == 240
replace competition = .4317423 if district_code == 244
replace competition = .4500548 if district_code == 246
replace competition = .4789177 if district_code == 250
replace competition = .4815606 if district_code == 252
replace competition = .4501251 if district_code == 256
replace competition = .4574504 if district_code == 258
replace competition = .4574827 if district_code == 260
replace competition = .4342267 if district_code == 264
replace competition = .4022278 if district_code == 300
replace competition = .4736422 if district_code == 304
replace competition = .4744546 if district_code == 306
replace competition = .4415579 if district_code == 310
replace competition = .4809597 if district_code == 312
replace competition = .4615807 if district_code == 316
replace competition = .5064411 if district_code == 318
replace competition = .4638137 if district_code == 320
replace competition = .5185984 if district_code == 324
replace competition = .4375064 if district_code == 326
replace competition = .4913154 if district_code == 330
replace competition = .5260947 if district_code == 332
replace competition = .5089728 if district_code == 336
replace competition = .5072834 if district_code == 338
replace competition = .4343421 if district_code == 340
replace competition = .4832082 if district_code == 344
replace competition = .4450483 if district_code == 346
replace competition = .4723596 if district_code == 350
replace competition = .4619011 if district_code == 352
replace competition = .4569737 if district_code == 356
replace competition = .4027703 if district_code == 358
replace competition = .4586516 if district_code == 360
replace competition = .4685028 if district_code == 364
replace competition = .40375 if district_code == 366
replace competition = .3591497 if district_code == 370
replace competition = .4798088 if district_code == 380
replace competition = .4556401 if district_code == 390
replace competition = .4273239 if district_code == 454
replace competition = .4344673 if district_code == 460
replace competition = .4050625 if district_code == 466
replace competition = .472544 if district_code == 470
replace competition = .4181056 if district_code == 476
replace competition = .4071148 if district_code == 482
replace competition = .4913993 if district_code == 502
replace competition = .4944101 if district_code == 508
replace competition = .4769619 if district_code == 514
replace competition = .5110917 if district_code == 520
replace competition = .4640033 if district_code == 526
replace competition = .4808708 if district_code == 530
replace competition = .4875095 if district_code == 536
replace competition = .4750006 if district_code == 542
replace competition = .4681416 if district_code == 548
replace competition = .4402604 if district_code == 554
replace competition = .4662587 if district_code == 560
replace competition = .443675 if district_code == 566
replace competition = .3674319 if district_code == 570
replace competition = .4587817 if district_code == 576
replace competition = .4782746 if district_code == 582
replace competition = .4761395 if district_code == 588
replace competition = .4545875 if district_code == 594
replace competition = .4435342 if district_code == 602
replace competition = .4629624 if district_code == 608
replace competition = .4836758 if district_code == 614
replace competition = .4842347 if district_code == 620
replace competition = .4821287 if district_code == 626
replace competition = .376989 if district_code == 636
replace competition = .4626811 if district_code == 642
replace competition = .4973973 if district_code == 648
replace competition = .4738852 if district_code == 660
replace competition = .4657671 if district_code == 666
replace competition = .4659289 if district_code == 670
replace competition = .4473136 if district_code == 676
replace competition = .4739034 if district_code == 702
replace competition = .4928483 if district_code == 708
replace competition = .3867371 if district_code == 714
replace competition = .397323 if district_code == 720
replace competition = .4786504 if district_code == 726
replace competition = .4545491 if district_code == 730
replace competition = .3889886 if district_code == 736
replace competition = .430796 if district_code == 742
replace competition = .4781483 if district_code == 748
replace competition = .3622307 if district_code == 754
replace competition = .4693759 if district_code == 760
replace competition = .1745868 if district_code == 802
replace competition = .1889005 if district_code == 806
replace competition = .3663773 if district_code == 810
replace competition = .411398 if district_code == 814
replace competition = .4618716 if district_code == 818
replace competition = .3145535 if district_code == 822
replace competition = .4473829 if district_code == 826
replace competition = .45915 if district_code == 834
replace competition = .3778203 if district_code == 838
replace competition = .3725961 if district_code == 842
replace competition = .39216 if district_code == 850
replace competition = .2924904 if district_code == 854
replace competition = .2315169 if district_code == 858
replace competition = .4364653 if district_code == 902
replace competition = .4061989 if district_code == 906
replace competition = .4344332 if district_code == 914
replace competition = .4165753 if district_code == 918
replace competition = .4161319 if district_code == 922
replace competition = .410601 if district_code == 926
replace competition = .4291402 if district_code == 930
replace competition = .4598101 if district_code == 938


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen district_incumbent = .

replace district_incumbent = 241 if riding == "Abitibi Est"
replace district_incumbent = 241 if riding == "Abitibi Ouest"
replace district_incumbent = 243 if riding == "Acadie"
replace district_incumbent = 243 if riding == "Anjou Louis Riel"
replace district_incumbent = 241 if riding == "Argenteuil"
replace district_incumbent = 241 if riding == "Arthabaska"
replace district_incumbent = 241 if riding == "Beauce Nord"
replace district_incumbent = 241 if riding == "Beauce Sud"
replace district_incumbent = 241 if riding == "Beauharnois"
replace district_incumbent = 241 if riding == "Bellechasse"
replace district_incumbent = 241 if riding == "Berthier"
replace district_incumbent = 241 if riding == "Bertrand"
replace district_incumbent = 241 if riding == "Blainville"
replace district_incumbent = 246 if riding == "Bonaventure"
replace district_incumbent = 241 if riding == "Borduas"
replace district_incumbent = 243 if riding == "Bourassa Sauvé"
replace district_incumbent = 241 if riding == "Brome Missisquoi"
replace district_incumbent = 241 if riding == "Camille Laurin"
replace district_incumbent = 241 if riding == "Chambly"
replace district_incumbent = 241 if riding == "Champlain"
replace district_incumbent = 241 if riding == "Chapleau"
replace district_incumbent = 241 if riding == "Charlesbourg"
replace district_incumbent = 241 if riding == "Charlevoix Côte de Beaupré"
replace district_incumbent = 241 if riding == "Châteauguay"
replace district_incumbent = 241 if riding == "Chauveau"
replace district_incumbent = 241 if riding == "Chicoutimi"
replace district_incumbent = 246 if riding == "Chomedey"
replace district_incumbent = 241 if riding == "Chutes de la Chaudière"
replace district_incumbent = 241 if riding == "Côte du Sud"
replace district_incumbent = 243 if riding == "D'Arcy McGee"
replace district_incumbent = 241 if riding == "Deux Montagnes"
replace district_incumbent = 241 if riding == "Drummond Bois Francs"
replace district_incumbent = 241 if riding == "Dubuc"
replace district_incumbent = 242 if riding == "Duplessis"
replace district_incumbent = 243 if riding == "Fabre"
replace district_incumbent = 242 if riding == "Gaspé"
replace district_incumbent = 241 if riding == "Gatineau"
replace district_incumbent = 244 if riding == "Gouin"
replace district_incumbent = 241 if riding == "Granby"
replace district_incumbent = 241 if riding == "Groulx"
replace district_incumbent = 244 if riding == "Hochelaga Maisonneuve"
replace district_incumbent = 243 if riding == "Hull"
replace district_incumbent = 241 if riding == "Huntingdon"
replace district_incumbent = 245 if riding == "Iberville"
replace district_incumbent = 242 if riding == "Îles de la Madeleine"
replace district_incumbent = 243 if riding == "Jacques Cartier"
replace district_incumbent = 244 if riding == "Jean Lesage"
replace district_incumbent = 241 if riding == "Jean Talon"
replace district_incumbent = 243 if riding == "Jeanne Mance Viger"
replace district_incumbent = 241 if riding == "Johnson"
replace district_incumbent = 242 if riding == "Joliette"
replace district_incumbent = 242 if riding == "Jonquière"
replace district_incumbent = 241 if riding == "La Peltrie"
replace district_incumbent = 243 if riding == "La Pinière"
replace district_incumbent = 241 if riding == "La Prairie"
replace district_incumbent = 241 if riding == "Labelle"
replace district_incumbent = 241 if riding == "Lac Saint Jean"
replace district_incumbent = 243 if riding == "LaFontaine"
replace district_incumbent = 243 if riding == "Laporte"
replace district_incumbent = 241 if riding == "L'Assomption"
replace district_incumbent = 244 if riding == "Laurier Dorion"
replace district_incumbent = 243 if riding == "Laval des Rapides"
replace district_incumbent = 241 if riding == "Laviolette Saint Maurice"
replace district_incumbent = 241 if riding == "Les Plaines"
replace district_incumbent = 241 if riding == "Lévis"
replace district_incumbent = 241 if riding == "Lotbinière Frontenac"
replace district_incumbent = 241 if riding == "Louis Hébert"
replace district_incumbent = 243 if riding == "Marguerite Bourgeoys"
replace district_incumbent = 241 if riding == "Marie Victorin"
replace district_incumbent = 243 if riding == "Marquette"
replace district_incumbent = 241 if riding == "Maskinongé"
replace district_incumbent = 241 if riding == "Masson"
replace district_incumbent = 242 if riding == "Matane Matapédia"
replace district_incumbent = 246 if riding == "Maurice Richard"
replace district_incumbent = 241 if riding == "Mégantic"
replace district_incumbent = 244 if riding == "Mercier"
replace district_incumbent = 243 if riding == "Mille Îles"
replace district_incumbent = 241 if riding == "Mirabel"
replace district_incumbent = 243 if riding == "Mont Royal Outremont"
replace district_incumbent = 241 if riding == "Montarville"
replace district_incumbent = 241 if riding == "Montmorency"
replace district_incumbent = 243 if riding == "Nelligan"
replace district_incumbent = 241 if riding == "Nicolet Bécancour"
replace district_incumbent = 243 if riding == "Notre Dame de Grâce"
replace district_incumbent = 241 if riding == "Orford"
replace district_incumbent = 241 if riding == "Papineau"
replace district_incumbent = 241 if riding == "Pointe aux Trembles"
replace district_incumbent = 243 if riding == "Pontiac"
replace district_incumbent = 241 if riding == "Portneuf"
replace district_incumbent = 241 if riding == "Prévost"
replace district_incumbent = 242 if riding == "René Lévesque"
replace district_incumbent = 241 if riding == "Repentigny"
replace district_incumbent = 241 if riding == "Richelieu"
replace district_incumbent = 241 if riding == "Richmond"
replace district_incumbent = 246 if riding == "Rimouski"
replace district_incumbent = 241 if riding == "Rivière du Loup Témiscouata"
replace district_incumbent = 243 if riding == "Robert Baldwin"
replace district_incumbent = 241 if riding == "Roberval"
replace district_incumbent = 244 if riding == "Rosemont"
replace district_incumbent = 241 if riding == "Rousseau"
replace district_incumbent = 244 if riding == "Rouyn Noranda Témiscamingue"
replace district_incumbent = 241 if riding == "Saint François"
replace district_incumbent = 243 if riding == "Saint Henri Sainte Anne"
replace district_incumbent = 241 if riding == "Saint Hyacinthe"
replace district_incumbent = 241 if riding == "Saint Jean"
replace district_incumbent = 241 if riding == "Saint Jérôme"
replace district_incumbent = 243 if riding == "Saint Laurent"
replace district_incumbent = 244 if riding == "Sainte Marie Saint Jacques"
replace district_incumbent = 241 if riding == "Sainte Rose"
replace district_incumbent = 241 if riding == "Sanguinet"
replace district_incumbent = 244 if riding == "Sherbrooke"
replace district_incumbent = 241 if riding == "Soulanges"
replace district_incumbent = 241 if riding == "Taillon"
replace district_incumbent = 244 if riding == "Taschereau"
replace district_incumbent = 241 if riding == "Terrebonne"
replace district_incumbent = 241 if riding == "Trois Rivières"
replace district_incumbent = 241 if riding == "Ungava"
replace district_incumbent = 241 if riding == "Vachon"
replace district_incumbent = 241 if riding == "Vanier Les Rivières"
replace district_incumbent = 243 if riding == "Vaudreuil"
replace district_incumbent = 241 if riding == "Verchères"
replace district_incumbent = 243 if riding == "Verdun"
replace district_incumbent = 243 if riding == "Viau"
replace district_incumbent = 243 if riding == "Vimont"
replace district_incumbent = 243 if riding == "Westmount Saint Louis"

gen reelected = .

replace reelected = 1 if district_outcome == district_incumbent
replace reelected = 0 if district_outcome!=district_incumbent

label define reelected 0 "Not reelected" 1 "Reelected"
label values reelected reelected


***************************
** INDEPENDENT VARIABLES **
***************************

* Number of days before election

generate surveydate = date(time, "YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20221003

gen electiondate=date(string(edate,"%8.0f"),"YMD") - 1

format %tdMon_DD,_CCYY electiondate

gen days = (electiondate - surveydate) 

replace days = . if days < 0

* Province

gen province = 6

label define province 6 "Quebec"
label values province province

* Language

gen language = langfr

label define language 0 "Non-francophone" 1 "Francophone"
label values language language

* Gender

* Age

gen r_age = age

replace r_age = . if r_age < 18
replace r_age = . if r_age > 100

* Age 55 and over

gen age55 = 1 if r_age >= 55
replace age55 = 0 if r_age < 55 & r_age!=.

label define age55 0 "Less than 55" 1 "55 and over"
label values age55 age55

* Education

gen education = .

replace education = 881 if ses_educ_none == 1
replace education = 882 if ses_educ_prim == 1
replace education = 883 if ses_educ_sec == 1
replace education = 884 if ses_educ_coll == 1
replace education = 885 if ses_educ_bacc == 1
replace education = 886 if ses_educ_master == 1
replace education = 887 if ses_educ_phd == 1

label define education 881 "None" 882 "Primary school" 883 "High school" 884 "College/CEGEP" 885 "Bachelor" 886 "Master" 887 "Doctorate"
label values education education

* University degree

gen univ = 1 if educuniv == 1
replace univ = 0 if educuniv == 0

label define univ 0 "No university degree" 1 "University degree"
label values univ univ

* Postgraduate degree

gen postgrad = 1 if education >= 886
replace postgrad = 0 if education < 886 & education!=.

label define postgrad 0 "No postgraduate degree" 1 "Postgraduate degree"
label values postgrad postgrad

* Postsecondary degree

gen postsec = 1 if education >= 884
replace postsec = 0 if education < 884 & education!=.

label define postsec 0 "No postsecondary" 1 "Postsecondary"
label values postsec postsec

* Household income

gen income = .

replace income = 881 if ses_income_none == 1
replace income = 882 if ses_income_i1to30 == 1
replace income = 883 if ses_income_i31to60 == 1
replace income = 884 if ses_income_i61to90 == 1
replace income = 885 if ses_income_i91to110 == 1
replace income = 886 if ses_income_i111to150 == 1
replace income = 887 if ses_income_i151to200 == 1
replace income = 888 if ses_income_i201toinf == 1

label define income 881 "No income" 882 "$1-$30,000" 883 "$30,001-$60,000" 884 "$60,001-$90,000" 885 "$90,001-$110,000" 886 "$110,001-$150,000" 887 "$150,001-$200,000" 888 "More than $200,000"
label values income income

* Household income (quintile)

xtile income5 = income, nq(5)

* Highest income categories

gen highinc = 1 if income5 == 5
replace highinc = 0 if income5 < 5 & income!=.

label define highinc 0 "Others" 1 "Highest incomes"
label values highinc highinc

* Minority

gen minority = ses_ethn_white

recode minority 0=1 1=0

label define minority 0 "Not visible minority" 1 "Visible minority"
label values minority minority

* Region

encode region_name, gen(regionQC)

* Vote choice

gen vote_choice = .

replace vote_choice = 241 if op_intent_caq == 1
replace vote_choice = 242 if op_intent_pq == 1
replace vote_choice = 243 if op_intent_plq == 1
replace vote_choice = 244 if op_intent_qs == 1
replace vote_choice = 245 if op_intent_pcq == 1
replace vote_choice = 88 if op_intent_other == 1
replace vote_choice = 99 if op_intent_no_vote == 1

label define vote_choice 241 "Coalition Avenir Quebec" 242 "Parti Quebecois" 243 "Quebec Liberal Party" 244 "Quebec solidaire" 245 "Parti conservateur du Quebec" 88 "Other party" 99 "No vote/Spoiled ballot"
label values vote_choice vote_choice

* Vote status (district)

gen vote_district = 1 if vote_choice == district_outcome
replace vote_district = 0 if vote_choice!=district_outcome & vote_choice!=. 

label define winner 0 "Voted for loser" 1 "Voted for winner"
label values vote_district winner


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecast_district = .

replace forecast_district = 241 if people_pred_caq == 1
replace forecast_district = 242 if people_pred_pq == 1
replace forecast_district = 243 if people_pred_plq == 1
replace forecast_district = 244 if people_pred_qs == 1
replace forecast_district = 245 if people_pred_pcq == 1

label define forecast 241 "Coalition Avenir Quebec" 242 "Parti Quebecois" 243 "Quebec Liberal Party" 244 "Quebec solidaire" 245 "Parti conservateur du Quebec"
label values forecast_district forecast

* Identify correct forecasts

gen correct_district = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district = 1 if forecast_district == district_outcome & forecast_district!=.

label define correct 0 "Incorrect" 1 "Correct"
label values correct_district correct


*************
** WEIGHTS **
*************

* Sex

gen sex_qc2022_w = male

recode sex_qc2022_w 0=1 1=2

label define sex_qc2022_w 1 "Female" 2 "Male"
label values sex_qc2022_w sex_qc2022_w

* Age

gen age_qc2022_w = r_age

replace age_qc2022_w = 1 if age_qc2022_w >= 18 & age_qc2022_w <= 19
replace age_qc2022_w = 2 if age_qc2022_w >= 20 & age_qc2022_w <= 24
replace age_qc2022_w = 3 if age_qc2022_w >= 25 & age_qc2022_w <= 29
replace age_qc2022_w = 4 if age_qc2022_w >= 30 & age_qc2022_w <= 34
replace age_qc2022_w = 5 if age_qc2022_w >= 35 & age_qc2022_w <= 39
replace age_qc2022_w = 6 if age_qc2022_w >= 40 & age_qc2022_w <= 44
replace age_qc2022_w = 7 if age_qc2022_w >= 45 & age_qc2022_w <= 49
replace age_qc2022_w = 8 if age_qc2022_w >= 50 & age_qc2022_w <= 54
replace age_qc2022_w = 9 if age_qc2022_w >= 55 & age_qc2022_w <= 59
replace age_qc2022_w = 10 if age_qc2022_w >= 60 & age_qc2022_w <= 64
replace age_qc2022_w = 11 if age_qc2022_w >= 65 & age_qc2022_w <= 69
replace age_qc2022_w = 12 if age_qc2022_w >= 70 & age_qc2022_w <= 74
replace age_qc2022_w = 13 if age_qc2022_w >= 75 & age_qc2022_w <= 79
replace age_qc2022_w = 14 if age_qc2022_w >= 80 & age_qc2022_w <= 84
replace age_qc2022_w = 15 if age_qc2022_w >= 85

label define age_qc2022_w 1 "18-19" 2 "20-24" 3 "25-29" 4 "30-34" 5 "35-39" 6 "40-44" 7 "45-49" 8 "50-54" 9 "55-59" 10 "60-64" 11 "65-69" 12 "70-74" 13 "75-79" 14 "80-84" 15 "85+"
label values age_qc2022_w age_qc2022_w

* Education

gen education_qc2022_w = education

recode education_qc2022_w 881=1 882=1 883=2 884=3 885=4 886=5 887=5

label define education_qc2022_w 1 "Below high school" 2 "High school" 3 "Postsecondary below bachelor" 4 "University undergraduate degree" 5 "University graduate degree"
label values education_qc2022_w education_qc2022_w

* Income

gen income_qc2022_w = income

recode income_qc2022_w 881=1 882=1 883=2 884=3 885=4 886=5 887=6 888=7

label define income_qc2022_w 1 "Less than $30,000" 2 "$30,001-$60,000" 3 "$60,001-$90,000" 4 "$90,001-$110,000" 5 "$110,001-$150,000" 6 "$150,001-$200,000" 7 "More than $200,000"
label values income_qc2022_w income_qc2022_w


**********
** SAVE **
**********

save "qc2022_datagochi.dta", replace

log close